﻿Imports System.Collections.ObjectModel
Imports System.Windows.Threading
Imports System.ComponentModel
Imports System.Security.Cryptography
Imports System.Threading.Tasks
Imports FuncClasses

Public Class UserManagementViewModel
    Implements INotifyPropertyChanged

    Private _edit As Boolean = False
    Private _dp As Dispatcher
    Private _db As FuncClasses.IDatabase
    Private _usr As User
    Public Sub New(dp As Dispatcher, user As FuncClasses.User, database As IDatabase)
        _dp = dp
        _usr = user
        _db = database
        _edit = user.CanEditUsers
        NotifyPropertyChanged("EditAllowed")
        Task.Factory.StartNew(AddressOf LoadUsers)
    End Sub

    Public Property UserList As ObservableCollection(Of UserModel)
    Public ReadOnly Property EditAllowed
        Get
            Return _edit
        End Get
    End Property

    Private Sub LoadUsers()
        Dim l = _db.GetAllUsers
        _dp.BeginInvoke(Sub()
                            UserList = New ObservableCollection(Of UserModel)
                            For Each item In l
                                UserList.Add(UserModel.FromUserPreviewModel(item))
                            Next
                            NotifyPropertyChanged("UserList")
                        End Sub)
    End Sub

    Protected Sub NotifyPropertyChanged(info As [String])
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
    End Sub

    Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

    Public Sub AddNewUser(name As String, id As String, password As String, cro As Boolean, cwo As Boolean, cra As Boolean, cwa As Boolean, crc As Boolean, cwc As Boolean, crd As Boolean, cwd As Boolean, cu As Boolean)
        If EditAllowed Then
            Dim hash As String = GetHash(name + password + (cro.ToString + cwo.ToString + cra.ToString + cwa.ToString + crc.ToString + cwc.ToString + crd.ToString + cwd.ToString + cu.ToString).ToLower)
            Dim user As New nHibernate.Models.UserPreviewModel With {.Hash = hash, .UserName = name, .UserID = id, .CanEditUsers = cu, .CanReadArticle = cra, .CanReadCustomer = crc, .CanReadDatabase = crd, _
                                                                     .CanReadOrder = cro, .CanWriteArticle = cra, .CanWriteCustomer = crc, .CanWriteDatabase = cwd, .CanWriteOrder = cwo}
            _db.SaveOrUpdateUser(user, _usr)
            LoadUsers()
        End If
    End Sub

    Public Function UserExists(value As String) As Boolean
        Return _db.UserExists(value)
    End Function

    Public Function UserIDExists(value As String) As Boolean
        Return _db.UserIDExists(value)
    End Function

    ''' <summary>
    ''' Creates the hash
    ''' </summary>
    ''' <param name="input"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function GetHash(input As String) As String
        Dim dataToMAC As Byte() = System.Text.Encoding.UTF8.GetBytes(input)
        Using hmacsha1 As New HMACSHA256(System.Text.Encoding.UTF8.GetBytes("EBFD359A91C23FEC2E3267D5FAE87A37F7156C0B0524CBDE533225F10605C913"))
            Return System.Convert.ToBase64String(hmacsha1.ComputeHash(dataToMAC))
        End Using
    End Function
End Class
